package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.myapplication.db.BlogDBHelper;
import com.example.myapplication.provider.BlogContract;
import java.util.ArrayList;
import java.util.List;

public class BlogActivity extends AppCompatActivity {

    private BlogDBHelper mDbHelper;
    private EditText etBlogTitle, etBlogContent;
    private Button btnAddBlog;
    private ListView lvBlogs;
    private BlogAdapter adapter; // 定义BlogAdapter
    private List<Blog> blogList; // 定义博客列表
    private Button btnProfile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog);

        mDbHelper = new BlogDBHelper(this);

        etBlogTitle = findViewById(R.id.etBlogTitle);
        etBlogContent = findViewById(R.id.etBlogContent);
        btnAddBlog = findViewById(R.id.btnAddBlog);
        lvBlogs = findViewById(R.id.lvBlogs);
        btnProfile = findViewById(R.id.btnProfile);

        btnProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(BlogActivity.this, ProfileActivity.class);
                startActivity(intent);
            }
        });

        btnAddBlog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addBlog();
            }
        });

        lvBlogs.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                deleteBlog(id);
                return true;
            }
        });

        blogList = new ArrayList<>(); // 初始化博客列表
        adapter = new BlogAdapter(this, blogList); // 初始化BlogAdapter
        lvBlogs.setAdapter(adapter);

        loadBlogs();
    }

    private void addBlog() {
        String title = etBlogTitle.getText().toString().trim();
        String content = etBlogContent.getText().toString().trim();

        if (!title.isEmpty() && !content.isEmpty()) {
            ContentValues values = new ContentValues();
            values.put(BlogContract.BlogEntry.COLUMN_NAME_TITLE, title);
            values.put(BlogContract.BlogEntry.COLUMN_NAME_CONTENT, content);

            SQLiteDatabase db = mDbHelper.getWritableDatabase();
            long newRowId = db.insert(BlogContract.BlogEntry.TABLE_NAME, null, values);
            if (newRowId == -1) {
                Toast.makeText(this, "添加失败", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                blogList.add(new Blog(newRowId, title, content)); // 添加到列表
                adapter.notifyDataSetChanged(); // 通知适配器数据变化
                etBlogTitle.setText(""); // 清空输入
                etBlogContent.setText("");
            }
        } else {
            Toast.makeText(this, "标题和内容不能为空", Toast.LENGTH_SHORT).show();
        }
    }

    private void deleteBlog(long id) {
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        int rowsDeleted = db.delete(
                BlogContract.BlogEntry.TABLE_NAME,
                BlogContract.BlogEntry.COLUMN_NAME_ID + " = ?",
                new String[]{String.valueOf(id)}
        );
        if (rowsDeleted == 0) {
            Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
            for (int i = 0; i < blogList.size(); i++) {
                if (blogList.get(i).getId() == id) {
                    blogList.remove(i); // 从列表中移除
                    break;
                }
            }
            adapter.notifyDataSetChanged(); // 通知适配器数据变化
        }
    }

    private void loadBlogs() {
        blogList.clear(); // 清空博客列表
        SQLiteDatabase db = mDbHelper.getReadableDatabase();
        Cursor cursor = db.query(BlogContract.BlogEntry.TABLE_NAME,
                new String[]{BlogContract.BlogEntry.COLUMN_NAME_ID,
                        BlogContract.BlogEntry.COLUMN_NAME_TITLE,
                        BlogContract.BlogEntry.COLUMN_NAME_CONTENT},
                null, null, null, null, null);

        while (cursor.moveToNext()) {
            long id = cursor.getLong(cursor.getColumnIndex(BlogContract.BlogEntry.COLUMN_NAME_ID));
            String title = cursor.getString(cursor.getColumnIndex(BlogContract.BlogEntry.COLUMN_NAME_TITLE));
            String content = cursor.getString(cursor.getColumnIndex(BlogContract.BlogEntry.COLUMN_NAME_CONTENT));
            blogList.add(new Blog(id, title, content)); // 添加到列表
        }
        cursor.close();
        db.close();
        adapter.notifyDataSetChanged(); // 通知适配器数据变化
    }
}